package com.baymax.test.scene.internal

import android.view.View
import kotlin.math.abs
import kotlin.properties.Delegates

class SizeTranslationWrapper(
    private val v: View,
    private val max: Int,
    private val style: Style,
    private val styleState: StyleListState,
) {
    val styles = styleState.styles
    private val record = style

    private val isEnd = this.record.left + record.width > 1600
    private val outViewPort = this.record.left + record.width + max > 1920

    //l , r
    private val partition =
        styles.minus(style).partition { (it.left + it.width) <= this.style.left + this.style.width }

    var width: Int by Delegates.observable(style.renderWidth) { p, o, n ->
        if (o != n) {
            val t = onChangeWith2(o, n)
            if (t) {
                v.requestLayout()
            }
        }
    }

    private fun onChangeWith2(o: Int, n: Int): Boolean {
        val zoom = n > record.renderWidth //放大
        println("isEnd $isEnd ,zoom $zoom , outViewPort $outViewPort ,$record")
        val reduce = abs(n - o)

        val self = styleState.maxWidthStyle == style
        if (!self) {
            if (zoom) {
                styleState.maxWidthStyle.renderWidth -= reduce
            } else {
                styleState.maxWidthStyle.renderWidth += reduce
            }
        }
        val left = if (!self) partition.first.minus(styleState.maxWidthStyle) else partition.first
        val right =
            if (!self) partition.second.minus(styleState.maxWidthStyle) else partition.second
        val maxLeft = styleState.maxLeft.minus(style)
        val maxRight = styleState.maxRight.minus(style)
        //max是否在当前的左侧
        val maxAtLeft = partition.first.contains(styleState.maxWidthStyle)

        if (zoom) {
            if (isEnd) {
                if (outViewPort) {
                    //左移
                    (if (self) left else left.minus(maxLeft)).forEach {
                        it.renderLeft -= reduce
                    }
                    style.renderLeft -= reduce
                } else {
                    (if (self) right else right.minus(maxRight)).forEach {
                        it.renderLeft += reduce
                    }
                }
            } else {
                //右移
                //right an not maxright
                val collect = if (self) right else if (maxAtLeft) right else right.plus(styleState.maxWidthStyle)
                (collect).forEach {
                    it.renderLeft += reduce
                }
            }
        } else {
            if (isEnd) {
                if (outViewPort) {
                    (if (self) left else left.minus(maxLeft)).forEach {
                        it.renderLeft += reduce
                    }
                    style.renderLeft += reduce
                } else {
                    //右移
                    (if (self) right else right.plus(styleState.maxWidthStyle)).forEach {
                        it.renderLeft -= reduce
                    }
                }
                //style.render_left -= reduce
            } else {
                (if (self) right else right.minus(maxRight)
                    .plus(styleState.maxWidthStyle)).forEach {
                    it.renderLeft -= reduce
                }
            }

        }
        this.style.renderWidth = n
        return true
    }


    private fun onChangeWith1(o: Int, n: Int): Boolean {
        val zoom = n > record.renderWidth //放大
        println("isEnd $isEnd ,zoom $zoom , outViewPort $outViewPort ,$record")
        val reduce = abs(n - o)
        if (zoom) {
            if (isEnd) {
                //左移
                if (outViewPort) {
                    partition.first.forEach {
                        it.renderLeft -= reduce
                    }
                    style.renderLeft -= reduce
                } else {
                    partition.second.forEach {
                        it.renderLeft += reduce
                    }
                }
            } else {
                //右移
                partition.second.forEach {
                    it.renderLeft += reduce
                }
            }
        } else {
            if (isEnd) {
                if (outViewPort) {
                    partition.first.forEach {
                        it.renderLeft += reduce
                    }
                    style.renderLeft += reduce
                } else {
                    partition.second.forEach {
                        it.renderLeft -= reduce
                    }
                }
                //style.render_left -= reduce
            } else {
                partition.second.forEach {
                    it.renderLeft -= reduce
                }
            }

        }
        this.style.renderWidth = n
        return true
    }

}